
package com.eyedog.video.videolist;

import com.eyedog.video.constant.Constants;
import com.eyedog.video.entity.AdvVideoEntity;
import com.eyedog.video.entity.VideoEntity;
import com.eyedog.video.http.HttpClientVideo;
import com.eyedog.video.http.ICallback;

import java.util.ArrayList;
import java.util.List;

public class VideoModel implements constract.IModel {

    private List<AdvVideoEntity> transVideoList(int categroyId, List<VideoEntity> videoEntities) {
        List<AdvVideoEntity> result = new ArrayList<>();
        if (videoEntities != null && videoEntities.size() > 0) {
            for (VideoEntity videoEntity : videoEntities) {
                AdvVideoEntity advVideoEntity = new AdvVideoEntity();
                advVideoEntity.setId(videoEntity.getId());
                advVideoEntity.setDesc(videoEntity.getDesc());
                advVideoEntity.setAuthor(videoEntity.getAuthor());
                advVideoEntity.setVod_name(videoEntity.getVod_name());
                advVideoEntity.setVod_pic(videoEntity.getVod_pic());
                advVideoEntity.setVod_url(videoEntity.getVod_url());
                result.add(advVideoEntity);
            }
        }
        return result;
    }

    @Override
    public void loadVideoList(final int page, final int categroyId,
            final ICallback<List<AdvVideoEntity>> resp) {
        if (resp != null) {
            List<VideoEntity> videoEntities = VideoInstance.getVideoFromCache(categroyId, page);
            if (videoEntities == null) {
                ICallback<List<VideoEntity>> videoListResp = new ICallback<List<VideoEntity>>() {
                    @Override
                    public void onLoadSuccess(List<VideoEntity> videoEntities) {
                        if (videoEntities == null) {
                            resp.onLoadFailed("null videoEntities");
                            return;
                        }
                        resp.onLoadSuccess(transVideoList(categroyId, videoEntities));
                        List<VideoEntity> cacheEntities = VideoInstance
                                .getVideoFromCache(categroyId);
                        if (cacheEntities != null) {
                            int startIndex = page * 10;
                            if (cacheEntities.size() > startIndex) {
                                List<VideoEntity> entities = cacheEntities.subList(0, startIndex);
                                entities.addAll(videoEntities);
                                VideoInstance.putVideoEntity(categroyId, entities);
                            } else if (cacheEntities.size() == startIndex) {
                                cacheEntities.addAll(videoEntities);
                                VideoInstance.putVideoEntity(categroyId, cacheEntities);
                            }
                        } else {
                            VideoInstance.putVideoEntity(categroyId, videoEntities);
                        }
                    }

                    @Override
                    public void onLoadFailed(String err) {
                        resp.onLoadFailed(err);
                    }
                };
                HttpClientVideo.getInstance().requestCategoryVideos(page, categroyId,
                        videoListResp);
            } else {
                resp.onLoadSuccess(transVideoList(categroyId, videoEntities));
            }
        }
    }

    @Override
    public void loadVideoList(final int page, String keywords,
            final ICallback<List<AdvVideoEntity>> resp) {
        if (resp != null) {
            ICallback<List<VideoEntity>> videoListResp = new ICallback<List<VideoEntity>>() {
                @Override
                public void onLoadSuccess(List<VideoEntity> videoEntities) {
                    if (videoEntities == null) {
                        resp.onLoadFailed("null videoEntities");
                        return;
                    }
                    resp.onLoadSuccess(transVideoList(Constants.Category_Empty, videoEntities));
                }

                @Override
                public void onLoadFailed(String err) {
                    resp.onLoadFailed(err);
                }
            };
            HttpClientVideo.getInstance().requestVideoList(page, keywords, videoListResp);
        }
    }

    @Override
    public void loadRecommendVideoList(final int page, final ICallback<List<AdvVideoEntity>> resp) {
        final int categroyId = Constants.Category_RecommendId;
        if (resp != null) {
            List<VideoEntity> videoEntities = VideoInstance.getVideoFromCache(categroyId, page);
            if (videoEntities == null) {
                ICallback<List<VideoEntity>> videoListResp = new ICallback<List<VideoEntity>>() {
                    @Override
                    public void onLoadSuccess(List<VideoEntity> videoEntities) {
                        if (videoEntities == null) {
                            resp.onLoadFailed("null videoEntities");
                            return;
                        }
                        resp.onLoadSuccess(transVideoList(categroyId, videoEntities));
                        List<VideoEntity> cacheEntities = VideoInstance
                                .getVideoFromCache(categroyId);
                        if (cacheEntities != null) {
                            int startIndex = (page - 1) * 10;
                            if (cacheEntities.size() > startIndex) {
                                List<VideoEntity> entities = cacheEntities.subList(0, startIndex);
                                entities.addAll(videoEntities);
                                VideoInstance.putVideoEntity(categroyId, entities);
                            } else if (cacheEntities.size() == startIndex) {
                                cacheEntities.addAll(videoEntities);
                                VideoInstance.putVideoEntity(categroyId, cacheEntities);
                            }
                        } else {
                            VideoInstance.putVideoEntity(categroyId, videoEntities);
                        }
                    }

                    @Override
                    public void onLoadFailed(String err) {
                        resp.onLoadFailed(err);
                    }
                };
                HttpClientVideo.getInstance().requestRecommendVideos(page, videoListResp);
            } else {
                resp.onLoadSuccess(transVideoList(categroyId, videoEntities));
            }
        }
    }

    @Override
    public void loadNewestVideoList(final int page, final ICallback<List<AdvVideoEntity>> resp) {
        final int categroyId = Constants.Category_NewId;
        if (resp != null) {
            List<VideoEntity> videoEntities = VideoInstance.getVideoFromCache(categroyId, page);
            if (videoEntities == null) {
                ICallback<List<VideoEntity>> videoListResp = new ICallback<List<VideoEntity>>() {
                    @Override
                    public void onLoadSuccess(List<VideoEntity> videoEntities) {
                        if (videoEntities == null) {
                            resp.onLoadFailed("null videoEntities");
                            return;
                        }
                        resp.onLoadSuccess(transVideoList(categroyId, videoEntities));
                        List<VideoEntity> cacheEntities = VideoInstance
                                .getVideoFromCache(categroyId);
                        if (cacheEntities != null) {
                            int startIndex = (page - 1) * 10;
                            if (cacheEntities.size() > startIndex) {
                                List<VideoEntity> entities = cacheEntities.subList(0, startIndex);
                                entities.addAll(videoEntities);
                                VideoInstance.putVideoEntity(categroyId, entities);
                            } else if (cacheEntities.size() == startIndex) {
                                cacheEntities.addAll(videoEntities);
                                VideoInstance.putVideoEntity(categroyId, cacheEntities);
                            }
                        } else {
                            VideoInstance.putVideoEntity(categroyId, videoEntities);
                        }
                    }

                    @Override
                    public void onLoadFailed(String err) {
                        resp.onLoadFailed(err);
                    }
                };
                HttpClientVideo.getInstance().requestNewestVideos(page, videoListResp);
            } else {
                resp.onLoadSuccess(transVideoList(categroyId, videoEntities));
            }
        }
    }

}
